/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.user.config;

import lombok.Data;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.remoting.protocol.heartbeat.MessageModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类 - 用户中心的通用消费者
 *
 * @author Steve Jrong
 * @since 1.0
 */
@Data
@Configuration
public class UserAppMQConsumerConfig {

    @Value("${waiter.user.rocketmq.name-server}")
    private String nameServerAddr;

    @Value("${waiter.user.rocketmq.consumer.group}")
    private String consumerGroup;

    /**
     * 专用于监听用户权限信息同步的消费者Bean
     *
     * @return
     */
    @Bean
    public MQPushConsumer waiterUserAppConsumer() {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup); // 创建消息消费者
        consumer.setNamesrvAddr(nameServerAddr); // 设置NameServer地址
        consumer.setInstanceName("waiter-user-app-consumer"); // 设置实例名称
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 设置程序首次启动时，从消息队列头获取数据
        consumer.setMessageModel(MessageModel.CLUSTERING); // 设置为集群消费模式
        // consumer.subscribe("Topic_Sync_Permission", "REQUEST_waiter-auth-component-test");
        return consumer;
    }
}